<template>
  <div class="commission-wrap">
    <el-form class="form">
      <el-form-item label-width="100px" label="姓名：">
        <el-input v-model="username" type="text"/>
      </el-form-item>
      <el-form-item label-width="120px" label="手机号：">
        <el-input v-model="phone" type="text"/>
      </el-form-item>
      <el-form-item label-width="100px" label="会员等级：">
        <el-select v-model="userlevel" placeholder="请选择">
          <el-option
            v-for="item in infos"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label-width="100px" label="消费时间：">
        <el-date-picker
          v-model="pickDate"
          :picker-options="pickerOptions2"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="left"/>
      </el-form-item>
      <el-form-item label-width="50px">
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" :summary-method="getSummaries" style="width: 100%" border show-summary>
      <el-table-column prop="index" label="序号" width="180"/>
      <el-table-column prop="username" label="姓名" width="200"/>
      <el-table-column prop="phone" label="手机号" width="200"/>
      <el-table-column prop="userlevel" label="等级" width="200">
        <template slot-scope="scope">
          <div>{{ scope.row.userlevel === 1 ? '普通会员' : scope.row.userlevel === 2 ? '服务商' : '区域服务商' }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="isagency" label="代理商" width="200">
        <template slot-scope="scope">
          <div>{{ scope.row.isagency === 1 ? '代理商' : '非代理商' }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="servicefee" label="服务费" width="200"/>
      <el-table-column prop="managementfee" label="管理费" width="200"/>
      <el-table-column prop="releaseuser" label="发布人" width="200"/>
      <el-table-column prop="consumptionmoney" label="消费金额" width="200"/>
      <el-table-column prop="optdate" label="消费时间" width="180">
        <template slot-scope="scope">
          <div>{{ new Date(scope.row.optdate).toLocaleString() }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="duration" label="购买时长" width="180">
        <template slot-scope="scope">
          <div>{{ scope.row.duration }}月</div>
        </template>
      </el-table-column>
      <el-table-column prop="transactionno" label="单号" width="180"/>
    </el-table>
    <pagination v-show="total>10" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog
      :visible.sync="centerDialogVisible"
      title="提示"
      width="30%"
      center>
      <span>确认删除？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination'
import { commissionList } from '@/api/order'
import { time } from '@/utils/index.js'
export default {
  name: 'Publish',
  components: {
    Pagination
  },
  data() {
    return {
      name: '',
      phone: '',
      infoValue: '',
      infos: [
        {
          value: 0,
          label: '全部'
        },
        {
          value: 1,
          label: '普通'
        },
        {
          value: 2,
          label: '服务商'
        },
        {
          value: 3,
          label: '区域服务商'
        },
        {
          value: 4,
          label: '代理商'
        }
      ],
      pickerOptions2: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      pickDate: '',
      total: 0,
      listQuery: {
        page: 1,
        limit: 10
      },
      centerDialogVisible: false,
      iDisplayStart: 0,
      iDisplayLength: 10,
      username: '',
      userlevel: 0,
      isagency: 2,
      list: [],
      totalcostamount: 0,
      serviceamount: 0,
      managementamount: 0
    }
  },
  computed: {
    tableData() {
      const result = []
      this.list.map((item, index) => {
        // const idx = this.type.findIndex((subitem) => {
        //   return item.typecode === subitem.code
        // })
        item = Object.assign({}, item, { index: `${(this.listQuery.page - 1) * this.listQuery.limit + 1 + index}`, edit: false })
        result.push(item)
      })
      return result
    }
  },
  created() {
    this.getCommissionList()
  },
  methods: {
    handleDelete(scope) {
      this.centerDialogVisible = true
    },
    getList(prop) {
      this.listQuery.page = prop.page
      this.listQuery.limit = prop.limit
      this.iDisplayStart = (prop.page - 1) * prop.limit
      this.iDisplayLength = prop.limit
      this.getCommissionList()
    },
    getSummaries(params) {
      const { columns } = params
      const SUM = {
        index: 0,
        service: 'serviceamount',
        management: 'managementamount',
        text: '合计'
      }
      const sums = []
      columns.map((item, index) => {
        if (index === SUM.index) {
          sums[index] = SUM.text
        }
        if (item.property === SUM.service) {
          sums[index] = `${this.serviceamount}元`
        }
        if (item.property === SUM.management) {
          sums[index] = `${this.managementamount}元`
        }
        sums.push('')
      })
      return sums
    },
    getCommissionList() {
      let fromtime, totime, isagency
      let { userlevel } = this
      const { iDisplayStart, iDisplayLength, username, pickDate, phone } = this
      if (!pickDate.length) {
        fromtime = ''
        totime = ''
      } else {
        fromtime = `${time(pickDate[0])} ${new Date(pickDate[0]).toTimeString().substring(0, 8)}`
        totime = `${time(pickDate[1])} ${new Date(pickDate[1]).toTimeString().substring(0, 8)}`
      }
      if (Number(userlevel) === 4) {
        isagency = 1
        userlevel = 0
      } else {
        isagency = 2
      }
      commissionList({ iDisplayStart, iDisplayLength, username, userlevel, isagency, phone, fromtime, totime })
        .then((res) => {
          const data = res.data
          this.list = data
          this.total = res.total
          this.serviceamount = res.serviceamount
          this.managementamount = res.managementamount
        }).catch((e) => {
          this.$message.error({
            message: '请稍后再试',
            duration: 1000
          })
        })
    },
    handleSearch() {
      this.getCommissionList()
    }
  }
}
</script>
<style lang="scss" scoped>
.commission-wrap {
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  .form {
    display: flex;
    flex-wrap: wrap;
  }
  .switch-btn {
    margin-top: 20px;
    margin-bottom: 20px;
    margin-left: 20px;
  }
}
</style>
